import time
import queue
import os
import cv2
import numpy as np

from MVSCam import *


def test_MVSCam():
    # time.sleep(10)
    qq = queue.Queue()
    def get_call(index):
        def frame_callback(im):
            #im = cv2.resize(im, (640, 480))
            #ss.plc.qq.put([index+1, im])
            print('frame_callback')
            if qq.qsize()<10:
                qq.put([index, im])
        return frame_callback

    ip = '192.168.1.100'
    cam = MVSCam(ip, 'off', get_call(0))
    #cam.open(11)
    for i in range(1000):
        try:
            if 1:
                if qq.qsize()>0:
                    xx = qq.get()
                else:
                    time.sleep(0.1)
                    print('time.sleep(0.1)')
                    continue
                index, im = xx
            else:
                im = cam.getFrame()
            im = cv2.resize(im, (640, 480))
            cv2.imshow('im', im)
            print(i)
            cv2.waitKey(10)
        except Exception as e:
            #i = random.randint(0, 1000) % 2
            print(f'qq.get err {str(e)}')
            continue

    cam.stop()
    return 0


if __name__ == "__main__":
    test_MVSCam()
